$(function () {
    findById();
});

/**
 * 根据id查询权限信息
 */

function findById() {
    let id = sessionStorage.getItem("permissionId");
    let result = myAjax("/api/rb/permission/findById", {id: id}, "get");
    if (result != null) {
        setData(result.data);
    }
}

//赋值
function setData(data) {
    $("#name").val(data.name);
    $("#value").val(data.value);
    $("#uri").val(data.uri);
    $("#type").val(data.type);
    $("#sort").val(data.sort);
    $("input[name='status'][value=" + data.status + "]").attr("checked", true);
}

//提交表单时再一次判断提交数据是否正确
function save() {
    let data = new FormData();
    data.append("id", sessionStorage.getItem("permissionId"));
    data.append("name", $("#name").val());
    data.append("value", $("#value").val());
    data.append("uri", $("#uri").val());
    data.append("type", $("#type").val());
    data.append("sort", $("#sort").val());
    data.append("status", $("input:radio[name='status']:checked").val());
    //拿到的图片有可能是一组数据，[0].files[0]选中当前对象的第一个数据
    data.append("file", $("#icon")[0].files[0]);
    let result = myImgAjax("/api/rb/permission/update", data,"post");
    if(result.code === 200){
        alert("更新成功（== ^^ ==）！")
        x_admin_close();
        x_admin_father_reload();
        // }else if(result.result == "noLogin"){
        //     // window.open("/html/login.html","_self");
        //     x_admin_close();
        //     x_admin_father_reload();
    }else{
        alert("更新失败！");
    }
}


